﻿namespace SLClient.ViewModels
{
    using SLClient.CustomerControls;
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Input;

    public class ProfitListViewModel : ViewModelBase
    {
        private Visibility _detailesVisibity = Visibility.Collapsed;
        private DateTime _endTime;
        private Visibility _processVisibity = Visibility.Visible;
        private Visibility _reportlistVisibity = Visibility.Visible;
        private SearchUserWindow _searchUserWindow;
        private ProfitList _selectProfitList;
        private ViwProfitReport _selectProfitReport = new ViwProfitReport();
        private string _selectProfitType;
        private DateTime _startTime;

        public ProfitListViewModel()
        {
            this.ViwProfitReports = new ObservableCollection<ViwProfitReport>();
            this.UserProfitDetailes = new ObservableCollection<ProfitList>();
            this.ReportCollection = new PagedCollectionView(this.ViwProfitReports);
            this.UserProfitCollection = new PagedCollectionView(this.UserProfitDetailes);
            this._startTime = DateTime.Now;
            this._endTime = DateTime.Now;
            this.CommandInit();
            this.Init(ViewDispatcher.LoginUser.UserId);
        }

        private void CommandInit()
        {
        }

        public void GetChildLowerList(Guid navguid, ref int postion, ref List<NavUserInfo> list)
        {
            Func<NavUserInfo, bool> func = null;
            if (navguid != Guid.Empty)
            {
                this.GetCurrentLower(navguid);
                postion = 0;
                if (navguid == ViewDispatcher.LoginUser.UserId)
                {
                    list = new List<NavUserInfo>();
                }
                else
                {
                    if (func == null)
                    {
                         func=(s)=>{return s.UserId==navguid;};
                    }
                    NavUserInfo item = Enumerable.First<NavUserInfo>(list, func);
                    int index = list.IndexOf(item);
                    int count = list.Count;
                    list.RemoveRange(index + 1, count - 1);
                }
            }
            else if ((((this._selectProfitReport.UserId != Guid.Empty) && (this._selectProfitReport.UserId != ViewDispatcher.LoginUser.UserId)) && (this._selectProfitReport.UserType == 0)) && (this._selectProfitReport.Tag != 1))
            {
                this.GetCurrentLower(this._selectProfitReport.UserId);
                postion = 0;
                NavUserInfo info2 = new NavUserInfo {
                    UserId = this._selectProfitReport.UserId,
                    UserName = this._selectProfitReport.UserAccount
                };
                list.Add(info2);
            }
        }

        public void GetCurrentLower(Guid userId)
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetProfitReportByUserIdAndTimeCompleted += delegate (object obj, GetProfitReportByUserIdAndTimeCompletedEventArgs arg) {                
                this.ViwProfitReports.Clear();
                for (int j = 0; j < arg.Result.Count; j++)
                {
                    if (j == 0)
                    {
                        arg.Result[j].CanSeeOperation = Visibility.Visible;
                    }
                    else
                    {
                        arg.Result[j].CanSeeOperation = Visibility.Collapsed;
                    }
                    this.ViwProfitReports.Add(arg.Result[j]);
                }
                Guid currentParentGuid = new Guid(arg.UserState.ToString());
                Enumerable.FirstOrDefault<ViwProfitReport>(this.ViwProfitReports,
                     new Func<ViwProfitReport, bool>((s) => { return s.UserId == currentParentGuid; })).Tag = 1;     
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.GetProfitReportByUserIdAndTimeAsync(userId, this.StartTime, this.EndTime, userId);
        }

        public void Init(Guid userId)
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetProfitReportByUserIdAndTimeCompleted += delegate (object obj, GetProfitReportByUserIdAndTimeCompletedEventArgs arg) {
                this.ViwProfitReports.Clear();
                for (int j = 0; j < arg.Result.Count; j++)
                {
                    if (j == 0)
                    {
                        arg.Result[j].CanSeeOperation = Visibility.Visible;
                    }
                    else
                    {
                        arg.Result[j].CanSeeOperation = Visibility.Collapsed;
                    }
                    this.ViwProfitReports.Add(arg.Result[j]);
                }
                this.ProcessVisibity = Visibility.Collapsed;
                Guid currentParentGuid = new Guid(arg.UserState.ToString());
                Enumerable.FirstOrDefault<ViwProfitReport>(this.ViwProfitReports, 
                   new Func<ViwProfitReport, bool>((s) => { return s.UserId == currentParentGuid; })).Tag = 1;                       
            };
            instance.GetProfitReportByUserIdAndTimeAsync(userId, this.StartTime, this.EndTime, userId);
        }
        private static Func<NavUserInfo, bool> myfunc;
        public void InitDetailes(ref int pos, ref int pos1, ref List<NavUserInfo> list)
        {
            pos = 0;
            pos1 = 0;
            this.ReportlistVisibity = Visibility.Visible;
            this.DetailesVisibity = Visibility.Visible;
            this.ProcessVisibity = Visibility.Visible;
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetProfitByUserIdAndTimeCompleted += delegate (object obj, GetProfitByUserIdAndTimeCompletedEventArgs arg) {
                this.UserProfitDetailes.Clear();
                foreach (ProfitList list1 in arg.Result)
                {
                    this.UserProfitDetailes.Add(list1);
                }
                this.ProcessVisibity = Visibility.Collapsed;
            };
            instance.GetProfitByUserIdAndTimeAsync(this.SelectProfitReport.UserId, this.StartTime, this.EndTime, this.SelectProfitType);
            if (myfunc == null)
            {
                myfunc = ((s)=>{return ((s.UserName == "盈亏明细") && (s.UserId == Guid.Empty));});
            }
            if (Enumerable.Count<NavUserInfo>(list, myfunc) == 0)
            {
                NavUserInfo item = new NavUserInfo {
                    UserId = Guid.Empty,
                    UserName = "盈亏明细"
                };
                list.Add(item);
            }
        }

        public void InvokeUserPlayDeatiles(Guid userId)
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetProfitByUserIdAndTimeCompleted += delegate (object obj, GetProfitByUserIdAndTimeCompletedEventArgs arg) {
                this.UserProfitDetailes.Clear();
                foreach (ProfitList list in arg.Result)
                {
                    this.UserProfitDetailes.Add(list);
                }
                this.ProcessVisibity =Visibility.Collapsed;
            };
            instance.GetProfitByUserIdAndTimeAsync(userId, this.StartTime, this.EndTime, this.SelectProfitType);
        }

        private static Func<NavUserInfo, bool> myfunc1;
        public void NavClick(ref List<NavUserInfo> list)
        {
            if (this.DetailesVisibity == Visibility.Visible)
            {
                this.DetailesVisibity = Visibility.Collapsed;
                this.ReportlistVisibity = Visibility.Visible;
                if (myfunc1 == null)
                {
                   myfunc1 = ((s)=>{return ((s.UserName == "盈亏明细") && (s.UserId == Guid.Empty));});
                }
                if (Enumerable.Count<NavUserInfo>(list, myfunc1) > 0)
                {
                    list.RemoveAt(list.Count - 1);
                }
            }
        }

        public void RefreshList(ref int pos, ref List<NavUserInfo> list)
        {
            Guid userId;
            pos = 0;
            this.ProcessVisibity = 0;
            if (list.Count > 0)
            {
                userId = list[list.Count - 1].UserId;
            }
            else
            {
                userId = ViewDispatcher.LoginUser.UserId;
            }
            this.Init(userId);
        }

        public void RefreshRecord(ref int pos, ref List<NavUserInfo> list)
        {
            Guid userId;
            this.ProcessVisibity = 0;
            pos = 0;
            if (list.Count == 1)
            {
                userId = ViewDispatcher.LoginUser.UserId;
            }
            else
            {
                userId = list[list.Count - 2].UserId;
            }
            this.InvokeUserPlayDeatiles(userId);
        }

        public void Search(ref int pos, ref int pos1, ref List<NavUserInfo> list)
        {
            Guid userId;
            pos = pos1 = 0;
            this.ProcessVisibity = 0;
            if (this.ReportlistVisibity == 0)
            {
                if (list.Count > 0)
                {
                    userId = list[list.Count - 1].UserId;
                }
                else
                {
                    userId = ViewDispatcher.LoginUser.UserId;
                }
                this.Init(userId);
            }
            else
            {
                if (list.Count == 1)
                {
                    userId = ViewDispatcher.LoginUser.UserId;
                }
                else
                {
                    userId = list[list.Count - 2].UserId;
                }
                this.InvokeUserPlayDeatiles(userId);
            }
        }

        public void SearchList(ref int pos)
        {
            pos = 0;
            this._searchUserWindow = new SearchUserWindow();
            this._searchUserWindow.SearchHander += new EventHandler(this.SearchUserWindowSearchHander);
            this._searchUserWindow.Show();
        }

        private void SearchUserWindowSearchHander(object sender, EventArgs e)
        {
            PagedCollectionView reportCollection = this.ReportCollection;
            reportCollection.Filter = (Predicate<object>) Delegate.Combine(reportCollection.Filter, 
               new Func<UserInfo, bool>((s) => { return s.UserAccount.Contains(this._searchUserWindow.tbUserName.Text);}));
        }

        public void ShowOrder(ref int pos1, ref List<NavUserInfo> list)
        {
            if ((this.SelectProfitList.ProfitType != "充值") && !(this.SelectProfitList.ProfitType == "提现"))
            {
                this.ProcessVisibity = Visibility.Visible;
                pos1 = 0;
                ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
                instance.GetOrderDetailesCompleted += delegate (object obj, GetOrderDetailesCompletedEventArgs arg) {
                    new OrderWindow(arg.Result).Show();
                    this.ProcessVisibity = Visibility.Collapsed;
                };
                instance.GetOrderDetailesAsync(this.SelectProfitList.OrderId, true);
            }
        }

        private static Func<NavUserInfo, bool> myfunc2;
        public void Transition(ref List<NavUserInfo> list)
        {
            if (this._reportlistVisibity == Visibility.Visible)
            {
                this.ReportlistVisibity = Visibility.Collapsed;
                this.DetailesVisibity = Visibility.Visible;
                if (myfunc2 == null)
                {
                   myfunc2 = ((s)=>{return ((s.UserName == "盈亏明细") && (s.UserId == Guid.Empty));});
                }
                if (Enumerable.Count<NavUserInfo>(list, myfunc2) == 0)
                {
                    NavUserInfo item = new NavUserInfo {
                        UserId = Guid.Empty,
                        UserName = "盈亏明细"
                    };
                    list.Add(item);
                }
            }
            else
            {
                this.ReportlistVisibity = Visibility.Visible;
                this.DetailesVisibity = Visibility.Collapsed;
                if (myfunc2 == null)
                {
                   myfunc2 = ((s)=>{return ((s.UserName == "盈亏明细") && (s.UserId == Guid.Empty));});
                }
                if (Enumerable.Count<NavUserInfo>(list, myfunc2) > 0)
                {
                    list.RemoveAt(list.Count - 1);
                }
            }
        }

        public Visibility DetailesVisibity
        {
            get
            {
                return this._detailesVisibity;
            }
            set
            {
                this._detailesVisibity = value;
                base.Notify<Visibility>(() => this.DetailesVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_DetailesVisibity)), new ParameterExpression[0]));
            }
        }

        public DateTime EndTime
        {
            get
            {
                return this._endTime;
            }
            set
            {
                this._endTime = value;
                base.Notify<DateTime>(() => this.EndTime);
                //base.Notify<DateTime>(System.Linq.Expressions.Expression.Lambda<Func<DateTime>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_EndTime)), new ParameterExpression[0]));
            }
        }

        public Visibility ProcessVisibity
        {
            get
            {
                return this._processVisibity;
            }
            set
            {
                this._processVisibity = value;
                base.Notify<Visibility>(() => this.ProcessVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_ProcessVisibity)), new ParameterExpression[0]));
            }
        }

        public List<string> ProfitTyies
        {
            get
            {
                List<string> list = new List<string> { "全部", "充值", "提现", "盈亏", "返点" };
                this._selectProfitType = list[0];
                return list;
            }
        }

        public ICommand RefreshRecordCommand { get; set; }

        public PagedCollectionView ReportCollection { get; set; }

        public Visibility ReportlistVisibity
        {
            get
            {
                return this._reportlistVisibity;
            }
            set
            {
                this._reportlistVisibity = value;
                base.Notify<Visibility>(() => this.ReportlistVisibity);
                //base.Notify<Visibility>(System.Linq.Expressions.Expression.Lambda<Func<Visibility>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_ReportlistVisibity)), new ParameterExpression[0]));
            }
        }

        public ICommand SearchCommand { get; set; }

        public ProfitList SelectProfitList
        {
            get
            {
                return this._selectProfitList;
            }
            set
            {
                this._selectProfitList = value;
                base.Notify<ProfitList>(() => this.SelectProfitList);
                //base.Notify<ProfitList>(System.Linq.Expressions.Expression.Lambda<Func<ProfitList>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_SelectProfitList)), new ParameterExpression[0]));
            }
        }

        public ViwProfitReport SelectProfitReport
        {
            get
            {
                return this._selectProfitReport;
            }
            set
            {
                this._selectProfitReport = value;
                base.Notify<ViwProfitReport>(() => this.SelectProfitReport);
                //base.Notify<ViwProfitReport>(System.Linq.Expressions.Expression.Lambda<Func<ViwProfitReport>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_SelectProfitReport)), new ParameterExpression[0]));
            }
        }

        public string SelectProfitType
        {
            get
            {
                return this._selectProfitType;
            }
            set
            {
                this._selectProfitType = value;
            }
        }

        public DateTime StartTime
        {
            get
            {
                return this._startTime;
            }
            set
            {
                this._startTime = value;
                base.Notify<DateTime>(() => this.StartTime);
                //base.Notify<DateTime>(System.Linq.Expressions.Expression.Lambda<Func<DateTime>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(ProfitListViewModel)), (MethodInfo)(ProfitListViewModel.get_StartTime)), new ParameterExpression[0]));
            }
        }

        public ICommand TransitionCommand { get; set; }

        public PagedCollectionView UserProfitCollection { get; set; }

        private ObservableCollection<ProfitList> UserProfitDetailes { get; set; }

        private ObservableCollection<ViwProfitReport> ViwProfitReports { get; set; }
    }
}

